﻿namespace Beetle
{
    using System;
    using System.Collections.Generic;

    public class MessagePool<T> where T: IMessage, new()
    {
        private Stack<T> _field_a;

        public MessagePool(int count)
        {
            this._field_a = new Stack<T>(0x400);
            for (int i = 0; i < count; i++)
            {
                T local = default(T);
                this._field_a.Push((local == null) ? Activator.CreateInstance<T>() : default(T));
            }
        }

        private T a()
        {
            if (default(T) != null)
            {
                return default(T);
            }
            return Activator.CreateInstance<T>();
        }

        public T Pop()
        {
            lock (((MessagePool<T>) this))
            {
                if (this._field_a.Count == 0)
                {
                    return this.a();
                }
                return this._field_a.Pop();
            }
        }

        public void Push(T item)
        {
            lock (((MessagePool<T>) this))
            {
                this._field_a.Push(item);
            }
        }
    }
}

